// pages/user/login/login.js
//获取应用实例
// 登录
var graceChecker = require('../../../utils/graceChecker.js');
import formRuleConfig from '../../../utils/formRule.config.js';
import api from '../../../api/index.js';
var CryptoJS = require('../../../utils/aesUtil.js')
var inputContent = {}; //输入内容
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0, // tab切换
    paracont: "获取验证码", //验证码文字
    vcdisabled: true, //验证码按钮状态
    verifycode: "", //返回的验证码
    phoneNum: '', //手机号码
    invitId: '', //团队邀请人id  没登陆时需要登录带id过来
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      invitId: options.invitId || ''
    })
  },
  /**
   * 点击tab切换
   */
  swichNav: function (e) {
    let index = e.target.dataset.current;
    if (index !== this.data.currentTab) {
      this.setData({
        currentTab: index
      })
    }
  },
  //登录
  loginSubmit(e) {
    let tabId = e.currentTarget.dataset.tab;
    let parmas = e.detail.value;
    console.log('11111111', parmas);
    let that = this;
    let obj = {};
    //判断登录方式
    if (tabId == 0) {
      //验证码登录
      if (!that.verifyFormat(parmas, 0)) return;

      //通过AES加密参数
      obj = {
        username: CryptoJS.AesEncrypt(parmas.username),
        code: CryptoJS.AesEncrypt(parmas.code),
      }

    } else {
      //账号密码登录
      if (!that.verifyFormat(parmas, 1)) return;
      //通过AES加密参数
      obj = {
        password: CryptoJS.AesEncrypt(parmas.password),
        username: CryptoJS.AesEncrypt(parmas.username),
      }
    }
    //去登录
    api.autoLign(obj).then(res => {
      console.log(res)
      wx.setStorageSync('token', res.data.data.loginToken);
      wx.setStorageSync('logInfo', JSON.stringify(res.data.data.logInfo));
      if (this.data.invitId) {
       wx.navigateTo({
         url: `/pages/scan-join/index?scene=${this.data.invitId}`,
       })
      } else {
        // 去首页
        wx.switchTab({
          url: '/pages/index/index',
        })
      }

    }).catch(() => {})
  },

  //验证格式
  verifyFormat(parmas, type) {
    //验证格式
    let checkRes = {};
    //登录方式  0 验证码登录  1 账号密码登录
    if (type == 0) {
      checkRes = graceChecker.check(parmas, formRuleConfig.loginCodeRule);
    } else {
      checkRes = graceChecker.check(parmas, formRuleConfig.loginRule);
    }
    if (!checkRes) {
      wx.showToast({
        title: graceChecker.error,
        icon: "none"
      });
      return false;
    }
    return true;
  },

  //获取用户输入 手机格式
  bindChange(e) {
    // console.log(e);
    let that = this;
    inputContent = e.detail.value;
    if ((/^1[3456789]\d{9}$/.test(inputContent))) {
      that.setData({
        vcdisabled: false,
      })
    } else if (!(/^1[3456789]\d{9}$/.test(inputContent))) {
      that.setData({
        vcdisabled: true
      })
    }
  },
  //获取验证码
  getVerifyCode() {
    let that = this;
    var second = 60,
      timePromise = undefined;
    timePromise = setInterval(function () {
      if (second <= 0 && (/^1[3456789]\d{9}$/.test(inputContent))) {
        clearInterval(timePromise);
        that.setData({
          paracont: "获取验证码",
          vcdisabled: false
        })
      } else {
        if (second <= 0) {
          clearInterval(timePromise);
          that.setData({
            paracont: "获取验证码",
            vcdisabled: false
          })
        } else {
          that.setData({
            paracont: second + "秒后重试",
            vcdisabled: true
          })
        }

        second--;
      }
    }, 1000, 122);

    //获取验证码
    api.getVerCode(inputContent);
  },

  //微信登录
  getWXLogin() {
    console.log('1111111111');
    wx.login({
      success: (res) => {
        console.log(res);
        let code = res.code
        if (code) {
          this.goWXLogin(code);
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  //微信-去登录
  goWXLogin(code) {
    //发起网络请求
    api.wxLogin(code).then(res => {
      if (res.data.code == 405) {
        wx.showModal({
          title: '提示',
          content: res.data.message,
          success: (rt) => {
            if (rt.confirm) {
              //绑定账号
              wx.navigateTo({
                url: `/pages/user/binding-weChat/index?openId=${res.data.data}`,
              })
            }
          }
        })

      } else {
        //登录
        wx.setStorageSync('token', res.data.data.DTG_MARK);
        wx.setStorageSync('logInfo', JSON.stringify(res.data.data.info));
        // 去首页
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
  },


})